Completed
Push — master ( a14198...1b9c43 )
by Andres
27s
created

elements.js ➔ ... ➔ ct.elementClass   A

Complexity

Conditions 4
Paths 4

Size

Total Lines 14

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 4
c 1
b 0
f 0
nc 4
dl 0
loc 14
rs 9.2
nop 1
1
/**
2
 elements
3
 Component that handles the periodic table tab.
4
 It includes the logic to purchase and display elements.
5
6
 @namespace Components
7
 */
8
'use strict';
9
10
angular.module('game').component('elements', {
11
  templateUrl: 'views/elements.html',
12
  controller: ['state', 'data', elements],
13
  controllerAs: 'ct'
14
});
15
16
function elements(state, data) {
17
  let ct = this;
18
  ct.state = state;
19
  ct.data = data;
20
21
  ct.elementPrice = function (element) {
22
    return Math.floor(Math.pow(data.constants.ELEMENT_PRICE_INCREASE, state.player.elements_unlocked) *
23
                              data.elements[element].number);
24
  };
25
26
   function isElementCostMet(element) {
27
    let price = ct.elementPrice(element);
28
    return state.player.resources.dark_matter.number >= price;
29
  }
30
31
  ct.buyElement = function (element) {
32
    if (state.player.elements[element].unlocked) {
33
      return;
34
    }
35
    if (isElementCostMet(element)) {
36
      let price = ct.elementPrice(element);
37
      state.player.resources.dark_matter.number -= price;
38
39
      state.player.elements[element].unlocked = true;
40
      state.player.elements[element].generators['1'] = 1;
41
      state.player.elements_unlocked++;
42
    }
43
  };
44
45
  /* This function returns the class that determines on which
46
  colour an element card */
47
  ct.elementClass = function (element) {
48
    if(!state.player.elements[element]){
49
      return 'element_unavailable';
50
    }
51
    if (state.player.elements[element].unlocked) {
52
      return 'element_purchased';
53
    }else{
54
      if(isElementCostMet(element)) {
55
        return 'element_cost_met';
56
      }else{
57
        return 'element_cost_not_met';
58
      }
59
    }
60
  };
61
62
  /* This function returns the class that determines the secondary
63
  colour of an element card */
64
  ct.elementSecondaryClass = function (element) {
65
    return ct.elementClass(element) + '_dark';
66
  };
67
}
68